﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;

namespace Lion.WeiXin.Controllers
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AsyHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {            
            context.Response.ContentType = "text/plain";
            try
            {
                if (context.Request.Params.Get("cmd") != null)
                {
                    string cmd = context.Request.Params.Get("cmd");
                    string targetClass = context.Request.Params.Get("target");
                    //string path = ConfigurationManager.AppSettings["HandlerClass"].ToString();
                    var obj = Activator.CreateInstance(Type.GetType("Lion.WeiXin.Controllers." + targetClass));
                    var method = Type.GetType("Lion.WeiXin.Controllers." + targetClass).GetMethod(cmd);


                    if (method != null)
                    {
                        //watch1.Stop();
                        var paras = method.GetParameters();
                        object[] requestArgList = new object[paras.Length];
                        for (int i = 0; i < paras.Length; i++)
                        {
                            var tType = paras[i].ParameterType;
                            object str = context.Request.Params.Get("params" + i);
                            //如果是字符串或者值类型，就把他改变类型
                            if (tType.Equals(typeof(string)))
                            {
                                requestArgList[i] = context.Request.Params.Get("params" + i);
                            }
                            else
                            {
                                requestArgList[i] = Convert.ChangeType(str, tType);
                            }
                        }
                        object strjson = method.Invoke(obj, requestArgList);
                        context.Response.Write(strjson);
                    }
                }
            }
            catch (Exception eError)
            {
                context.Response.Write(eError.Message);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
